$(function() {
	initBlockUI();
	updateTable(0, "#pager");
	initPagination();
	initCalendar();
	initModifyButton();
});

function initPagination()
{
	$("#pager").pagination($("#wholepage").val(), {
		items_per_page: 10,
		prev_text: "前一页",
		next_text: "后一页",
		callback: updateTable
	});
}

// 更新数据信息
function updateTable(page, pager_block)
{
	var url = "edit_person.php";
	var pars = { page_status: 2, page: (page + 1) };
	$.post(url, pars, function(xml) {
		$("#tbody2").html("");
		$("item", xml).each(function(i) {
			var id = $("id", this).text();
			var username = $("username", this).text();
			var regtime = $("register_time", this).text().split(" ")[0];
			$("<tr></tr>")
				.append($("<th></th>").html(i + 1))
				.append($("<td></td>").html($("<a href=\"#\"></a>")
					.html(username)
					.attr("userid", id)
					.attr("username", username)
					.click(function() {
						editPersonInfo($(this).attr("userid"), $(this).attr("username"), this);
						return false;
					})
				))
				.append($("<td></td>").html(regtime))
				.append($("<td></td>").attr("id", "status_" + id))
			.appendTo("#tbody2");
		});
		tablePretty("#tbody2");
	});
	return false;
}

// 显示员工信息
function editPersonInfo(id, username, handler)
{
	var url = "edit_person.php";
	var pars = { page_status: 1, id: id };
	var name = $(handler).html();
	$.post(url, pars, function(xml) {
		$("#identity").val($("identity", xml).text());
		$("input[@name='sex'][@value='" + $("sex", xml).text() + "']").attr("checked", "checked");
		$("#birth").val($("birth", xml).text());
		$("#tel").val($("tel", xml).text());
		$("#phone").val($("phone", xml).text());
		$("#qq").val($("qq", xml).text());
		$("#email").val($("email", xml).text());
		$("#address").val($("address", xml).text());
		$("input[@name='marriage'][@value='" + $("marriage", xml).text() + "']").attr("checked", "checked");
		$("#culture > option[@value='" + $("culture", xml).text() + "']").attr("selected", "selected");
		$("#degree").val($("degree", xml).text());
		$("#diploma_num").val($("diploma_num", xml).text());
		$("#graduate").val($("graduate", xml).text());
		$("#school").val($("school", xml).text());
		$("#spec_field").val($("spec_field", xml).text());
		$("#job_q_c").val($("job_q_c", xml).text());
		$("#grade").val($("grade", xml).text());
		$("#census_reg").val($("census_reg", xml).text());
		$("input[@name='census_class'][@value='" + $("census_class", xml).text() + "']").attr("checked", "checked");
		$("#modify_header").html("修改 " + name + " 的信息：");
		$("#modify_button").attr("userid", id).attr("username", username);
		$("#modify_block").modal();
	});
}

// 初始化日期表单
function initCalendar()
{
	$("#birth, #graduate").datepicker();
}

// 初始化“修改”按钮
function initModifyButton()
{
	$("#modify_button").click(function() {
		modifyDatas($(this).attr("userid"), $(this).attr("username"));
	});
}

// 提交修改后的数据到数据库
function modifyDatas(id, username)
{
	var url = "edit_person.php";
	var pars = {
		page_status: 	3,
		// id:				id,
		username:		username,
		identity:			$("#identity").val(),
		sex:				$("input[@name='sex']:checked").val(),
		birth:			$("#birth").val(),
		tel:				$("#tel").val(),
		phone:			$("#phone").val(),
		qq:				$("#qq").val(),
		email:			$("#email").val(),
		address:			$("#address").val(),
		marriage:			$("input[@name='marriage']:checked").val(),
		culture:			$("#culture").val(),
		degree:			$("#degree").val(),
		diploma_num:		$("#diploma_num").val(),
		graduate:			$("#graduate").val(),
		school:			$("#school").val(),
		spec_field:		$("#spec_field").val(),
		job_q_c:			$("#job_q_c").val(),
		grade:			$("#grade").val(),
		census_reg:		$("#census_reg").val(),
		census_class:		$("input[@name='census_class']:checked").val()
	};
	$.post(url, pars, function(text) {
		if (/^Success/.test(text))
		{
			$("#status_" + id).html($("<span></span>").html("修改成功！").css({color: "green"}).fadeOut(6000));
			m(0);
		}
	});
}

// 控制 modal 的隐现
function m(f) { if (f == 0) { $.modal.close(); } }

